//
//  MoreFeedBack.m
//  Trading365
//
//  Created by Jason on 12-4-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MoreFeedBack.h"
#import "Type.h"
#import <QuartzCore/QuartzCore.h>
#import "TaskManager.h"
#import "XmlRpc.h"
#import "myToast.h"


@implementation MoreFeedBack
@synthesize content;
@synthesize bgButton;
@synthesize placeHolder;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"意见反馈";
    
    self.view.backgroundColor=BACKGROUND_COLOR;
    self.content.frame = CGRectMake(15, 15, 290, 480-44-30);
    editRect = self.content.frame;
    
    [self.content setBackgroundColor:[UIColor clearColor]];
    
    [self.content.layer setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [self.content.layer setBorderColor:[NAVIGATION_BAR_COLOR CGColor]];
    [self.content.layer setBorderWidth:1.0];
    [self.content.layer setCornerRadius:10.0];
    [self.content.layer setMasksToBounds:YES];
    self.content.clipsToBounds = YES;

    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] 
                                initWithTitle:@"发送" 
                                style:UIBarButtonItemStylePlain
                                target:self 
                                action:@selector(save)]; 
    self.navigationItem.rightBarButtonItem = saveBtn;
    [saveBtn release];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    self.placeHolder.frame = CGRectMake(22, 19, 280, 30);
    self.placeHolder.hidden=NO;
    showPlaceHolder=YES;
    
}

- (void)viewDidUnload
{
    [self setContent:nil];
    [self setBgButton:nil];
    [self setPlaceHolder:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [content release];
    [bgButton release];
    [placeHolder release];
    [super dealloc];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.30f];  
    editRect = self.content.frame;
    self.content.frame = CGRectMake(15, 15, 290, 480-44-20-10-216-60);
    [UIView commitAnimations];
    
    if (showPlaceHolder) {
        self.placeHolder.hidden=YES;
        showPlaceHolder=NO;
    }
    
    return;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.30f];  
    self.content.frame = editRect;
    [UIView commitAnimations];
    return;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if([self.content.text length] == 0)
    {
        self.placeHolder.hidden=NO;
        showPlaceHolder=YES;
    }else{
        self.placeHolder.hidden=YES;
        showPlaceHolder=NO;        
    }
}

#pragma mark -myfun
-(void)save
{
    NSString *alert=@"";
    
    if ([content.text length] < 1)
        alert=@"请填写反馈内容.";
        
    if ([alert length]>0){
        UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:alert delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        [alertview release];
        return;
    }    
    
    [self.content resignFirstResponder];
    
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary *tmp;
    tmp = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
           content.text,@"content",nil];
    NSLog(@"tmp=%@",tmp);
    
    [xml SaveFeedBack:tmp];
    
    [tmp release];
    [xml release];
    
    [myToast showWithText:@"意见反馈发送成功."];

//    [self performSelector:@selector(goBack) withObject:nil afterDelay:5.0];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)hideKeyBoard:(id)sender {
    [self.content resignFirstResponder];
}
@end
